<template>
<!-- 头部区域 -->
  <el-container class="home-container">
    <el-header class="home-header">
      <div class="header-logo">
        <div><img src="../assets/login.png" alt=""></div>
        <span>首选后台管理系统</span>
      </div>
      <el-button @click="loginout" type="info">退出</el-button>
    </el-header>
      <!-- 主页面区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="istaggle ? '64px' : '200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#3778d7"
          unique-opened
          :collapse='istaggle'
          :collapse-transition='false'
          :router='true'
          :default-active="status"
          >
            <!-- 一级菜单 -->
          <el-submenu :index="items.id+''"
            v-for='items of menulist' :key='items.id'>
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconlist[items.id]"></i>
              <!-- 文本 -->
              <span>{{items.authName}}</span>
            </template>
            
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+items2.path"
              v-for='items2 of items.children' :key='items2.id'
              @click='savenavstate("/"+items2.path)'>
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{items2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data(){
    return{
      // 左侧菜单数据
      menulist:[],
      // menulist:[{id:1,authName:'用户管理',children:[{
      //             id:11,authName:'用户列表',path:'users'
      //           }]},{id:2,authName:'权限管理',children:[{
      //             id:21,authName:'权限管理1',path:'roles1'
      //           },{id:22,authName:'权限管理2',path:'roles2'
      //           }]},{id:3,authName:'商品管理',children:[{
      //             id:31,authName:'商品管理1',path:'shoop1'
      //           },{id:32,authName:'商品管理2',path:'shoop2'
      //           }]},{id:4,authName:'订单管理',children:[{
      //             id:41,authName:'订单管理1',path:'shoper1'
      //           }]},{id:5,authName:'数据统计',children:[{
      //             id:51,authName:'数据统计1',path:'data'
      //           }]}],
      iconlist:{
        '125':'iconfont icon-yonghuguanli',
        '103':'iconfont icon-quanxianguanli',
        '101':'iconfont icon-goods',
        '102':'iconfont icon-dingdanguanli',
        '145':'iconfont icon-shujutongji',
      },
      // 是否折叠
      istaggle:false,
      // 被激活的链接地址
      status:'',
    }
  },
  methods:{
    loginout(){
      sessionStorage.clear();
      this.$router.push('/login')
    },
    // 获取所有的菜单
    getMenuList(){
      new Promise((resolve,reject)=>{
        resolve(this.$axios.get('/menus'))
      }).then(res=>{
        if(res.data.meta.status !== 200) return this.$message.error(res.data.meta.meg)
        this.menulist=res.data.data
        // console.log(res)
      })
    },
    toggle(){
      this.istaggle=!this.istaggle
    },
    // 保存链接的激活状态
    savenavstate(status){
      // console.log(status)
      this.status=sessionStorage.setItem('status',status)
      this.status=status
    }
  },
  created(){
    this.getMenuList()
    this.status=sessionStorage.getItem('status')
  },
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background: #363d3f;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header-logo{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    color:#fff;
    img{
      display: block;
    }
    span{
      font-size: 20px;
    }
  }
}
.el-aside{
  background: #313743;
  .el-menu{
    border: none;
  }
  .iconfont:before{
    margin-right: 6px;
  }
}
.el-main{
  background: #e9edf0;
}
.toggle-button{
  background: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color:#fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
